package com.acwer.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import com.acwer.drools.domain.CHSDRG;
import com.acwer.drools.domain.JBBM;
import com.acwer.drools.domain.SSBM;

public class CollectionUtils {
	
	/**
	 * 求ls1,ls2前几位的交集
	 * @param ls1 集合1
	 * @param ls2 集合2
	 * @param subLen 前几位？
	 * @return
	 */
	public static List<String> inter(List<String> ls1,List<String> ls2,int subLen){
		
		List<String> rs1=ls1.stream().
				filter(it->!StringUtils.isEmpty(it)&&it.length()>=subLen).
				map(it->it.substring(0, subLen)).collect(Collectors.toList());
		
		List<String> rs2=ls2.stream().
				filter(it->!StringUtils.isEmpty(it)&&it.length()>=subLen).
				map(it->it.substring(0, subLen)).collect(Collectors.toList());
		
		List<String> prefs=rs1.stream().filter(it->rs2.contains(it)).collect(Collectors.toList());
		
		List<String> all=new ArrayList<String>();
		all.addAll(ls1);
		all.addAll(ls2);
		//System.out.println(all.stream().filter(it->prefs.contains(it.substring(0, subLen))).distinct().collect(Collectors.toList()));
		return all.stream().filter(it->prefs.contains(it.substring(0, subLen))).distinct().collect(Collectors.toList());
		
	}
	public static List<String> inter(List<String> ls1,List<String> ls2){
		return ls1.stream().filter(it->ls2.contains(it)).distinct().collect(Collectors.toList());
		
	}
	
public static List<String> inter(JBBM ls1,List<String> ls2,int subLen){
		
		List<String> rs1=new ArrayList<String>();
		rs1.add(ls1.getAdrg().substring(0, subLen));
			/*	ls1.stream().
				filter(it->!StringUtils.isEmpty(it)&&it.length()>=subLen).
				map(it->it.substring(0, subLen)).collect(Collectors.toList());*/
		
		List<String> rs2=ls2.stream().
				filter(it->!StringUtils.isEmpty(it)&&it.length()>=subLen).
				map(it->it.substring(0, subLen)).collect(Collectors.toList());
		
		List<String> prefs=rs2.stream().filter(it->rs1.contains(it)).collect(Collectors.toList());
		
		List<String> all=new ArrayList<String>();
		all.add(ls1.getAdrg());
		all.addAll(ls2);
		System.out.println(all.stream().filter(it->prefs.contains(it.substring(0, subLen))).distinct().collect(Collectors.toList()));
		return all.stream().filter(it->prefs.contains(it.substring(0, subLen))).distinct().collect(Collectors.toList());
		
	}
	
	
	
	public  static  List<String> interSs(JBBM ls1,List<SSBM> ls2,int type){
		List<String> rs2= new  ArrayList<String>();
		
			rs2.add(ls1.getAdrg().substring(0, 1));
		
		List<SSBM> ssbms=ls2.stream().filter(it->it.getType().equals(type)&&
				rs2.contains(it.getAdrg().substring(0, 1))).collect(Collectors.toList());
		
		List<String> all=new ArrayList<String>();
		//all.addAll(ssbms.stream().filter(it -> it.getAdrg())).collect(Collectors.toList());
		
		for(SSBM o: ssbms){
	    	
				all.add(o.getAdrg().toString());
	   }
		
		
		return all;
	}
	
	
	public static List<String> sort(List<String> ls1){
		return ls1.stream().sorted().limit(1).collect(Collectors.toList());
	}
	public  static String   get1Str(JBBM jbbm){
		
		return  jbbm.getAdrg().substring(0, 1);
				
				
			//chsDrgs.stream().filter(it->it.getDRG().substring(3,4).equals(pam)).collect(Collectors.toList());
		
	}
	
	
	
	public  static List<CHSDRG>  get4Str(List<CHSDRG> chsDrgs,String pam){
		System.out.println(pam);
		System.out.println(chsDrgs.get(0).getDRG().substring(3,4));
		System.out.println(chsDrgs.stream().map(CHSDRG::getDRG).collect(Collectors.toList()));
		return chsDrgs.stream().filter(it->it.getDRG().substring(3,4).equals(pam)).collect(Collectors.toList());
		
	}
	public  static  List<String> inter(List<SSBM> ls2){
		/*List<String> rs2= new  ArrayList<String>();
		
			rs2.add(ls1.getAdrg().substring(0, 1));
		
		List<SSBM> ssbms=ls2.stream().filter(it->it.getType().equals(type)&&
				rs2.contains(it.getAdrg().substring(0, 1))).collect(Collectors.toList());*/
		
		List<String> all=new ArrayList<String>();
		//all.addAll(ssbms.stream().filter(it -> it.getAdrg())).collect(Collectors.toList());
		
		for(SSBM o: ls2){
	    	if(o.getAdrg().matches("^[A-Z][A-J][0-9]$")){
	    		all.add(o.getAdrg().toString());
	    	}
	   }
		
		
		return all;
	}

	
	
}
